# @Author   : 懒羊羊
# @File     : wk_login.py
# @Time     : 2024/12/23 14:40
import json
import os

import requests
from jsonpath_ng import parse

from config import pits_path

# 登录
url = "http://192.168.88.100:8080/login"

mydata = {'username': 'admin', 'password': '123456'}
print(url)
r = requests.post(url, data=mydata)

print(r.text)

my_token = r.json().get("Admin-Token")
print(my_token)

# 文件上传
fileURL = "http://192.168.88.100:8080/file/upload"

headers = {'Admin-Token': my_token}

# 键值对参数
filetype = {"type": "file"}

# 文件参数
ffile = {"file": (
    'Snipaste_2024-12-09_09-28-00',
    open(r"D:\python-workspace\python-lizhe\python-base-trl\apiTest\datas\Snipaste_2024-12-09_09-28-00.png", "rb"),
    "image/png")}

r = requests.post(fileURL, data=filetype, files=ffile, headers=headers)

print(r.text)

# 获取batchId
bid = r.json().get("batchId")
print(bid)

# 新增日志
# addurl = "http://192.168.88.100:8080/OaLog/addOrUpdate"
#
# # 请求参数
# my_log_data = {
#     "categoryId": "2", "content": "999python", "tomorrow": "456", "question": "789",
#     "batchId": bid
# }
#
# # 将登录的token加入headers中
# my_headers = {
#     'Admin-Token': my_token
# }
#
# # 传入请求参数并添加token到请求头中
# r1 = requests.post(addurl, json=my_log_data, headers=my_headers)
#
# print(r1.text)

# 下载文件并保存
#
# pits_url = "http://192.168.88.100:8080/upload/20241223/ll.png"
#
# # 将文件保存到指定目录
# tp_name = os.path.join(pits_path, 'll2.png')
# print(tp_name)
#
# # 通过get获取
# r = requests.get(pits_url)
#
# # 通过二进制流写入文件 wb，r.content 是二进制
# with open(tp_name, "wb") as f:
#     f.write(r.content)


# 获取日志信息
# 得到所有响应内容，根据响应内容提取所需要的内容

# 获取日志列表
log_url = "http://192.168.88.100:8080/OaLog/queryList"

my_headers = {'Admin-Token': my_token}

r = requests.post(log_url, headers=my_headers)

print(r.text)

# 将 JSON 字符串解析为 Python 对象
data = json.loads(r.text)

# 定义 JSONPath 表达式
jsonpath_expr = parse('$.data.list[*].file[*].filePath')

# 提取路径
file_paths = [match.value for match in jsonpath_expr.find(data)]
print(file_paths)

# 遍历保存
# 循环请求该文件路径并写入我们项目目录下

